<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.itaoke.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  Caption <Caption@163.com> QQ 1674653
// +----------------------------------------------------------------------

class XuanController extends BackendController {
    public function _initialize() {
        parent::_initialize(); 
        
        $api_config = M('ItemsSite')->where(array('code' => 'itaoke'))->getField('config');
        $this->_tbconfig = unserialize($api_config);
    }
    public function index() {
        $this->display();
    }
    
    public function setting() {
        if (IS_POST) {

            if( $this->_tbconfig =='')
            	$this->ajaxReturn(0, "请正确配置爱淘客开放平台接口!");
            $this->collect();
        }
    }
    
    public function collect() {
    	
        $taodianjin = C('itk_taojindian_html');
        if (strpos($taodianjin, "text/javascript")) {
        	$pid = get_word($taodianjin, 'pid: "' , '"');
        } else {
        	$pid = $taodianjin;
        }
        if (!$pid) {
        	$this->ajaxReturn(0, '请设置淘点金后采集！否则无佣金');
        }
        
        $p = I('get.p','1');

        $tb_top = $this->_get_top();
        $req = $tb_top->load_api('ItaokeXuanGetRequest');

        $resp = (array)$tb_top->execute($req);
        if($resp['status'] != '0000')
        {
        	$this->ajaxReturn(0, $resp['msg']);
        }
        else
        {
        	if ($p == 1) {
        		$totalcoll = 0;
        	} else {
        		$this->ajaxReturn(0, "已经采集完成页！请返回，谢谢");
        	}
        	
        	$coll = 0;
			$item = $resp['data'];
			$totalnum = count($item);
			if($totalnum==0){
				$this->ajaxReturn(0, "已经采集完成页！请返回，谢谢");
			}
        	for($i=0;$i<$totalnum;$i++)
        	{
        		$_item['cate_id'] = $item[$i]['cid'];
        		$_item['num_iid'] = $item[$i]['num_iid'];
        		$_item['title'] = $item[$i]['title'];
        		$_item['tags'] = $item[$i]['tags'];
        		$_item['sellerId'] = $item[$i]['sellerid'];
        		$_item['pic_url'] = $item[$i]['pic_url'];
        		$_item['price'] = $item[$i]['price'] != '' ? $item[$i]['price'] :$item[$i]['coupon_price'];
        		$_item['volume'] = $item[$i]['sale_num'] == '' ? 0 : $item[$i]['sale_num'];
        		$_item['coupon_price'] = $item[$i]['coupon_price'];
        		$_item['coupon_rate']	= round(($_item['coupon_price']/$_item['price'])*10000, 0);
        		$_item['shop_type'] = $item[$i]['source_id'] == 1 ? 'C':'B';
        		$_item['item_url'] = $item[$i]['item_url'];
        		$_item['add_time'] = time();
        		$_item['isq'] = 1;
        		$_item['quan'] = $item[$i]['qprice'];
        		$_item['quanurl'] = $item[$i]['qwurl'];
        		$_item['quan_surplus'] = $item[$i]['qsnum'];
        		$_item['quan_receive'] = $item[$i]['qnum'] - $item[$i]['qsnum'];
       			$_item['commission_rate'] = $item[$i]['commission'];
        		$_item['nick']='爱淘客联盟';
        		$_item['coupon_start_time']=time();
        		$_item['coupon_end_time']=$item[$i]['qtime']+24*60*60-1;
        		$_item['ems'] = $item[$i]['ems'];
        		$_item['astime'] = date('Ymd');
        		$_item['dx'] = $item[$i]['plan_id'] == 2 ? 1 :0;
        		$_item['intro'] = $item[$i]['content'] ;
        		if(0 != $this->_ajax_tb_publish_insert($_item))
        			$coll++;
        		$totalcoll++;
        	}
//        	if ($coll == 0) {
//        		$this->ajaxReturn(0, "本次更新采集已完成，本页已经采集不重复~");
//        	}

        	F("totalcoll" , $totalcoll);
        	$this->assign("totalcoll" , $coll);				// 总个数
        	$resp = $this->fetch("collect");
        	$this->ajaxReturn(1, '', $resp);
        }
    }
    
    private function _ajax_tb_publish_insert($item) {
      return D('Items')->ajax_itk_publish($item);
    }

    private function _get_top() {
    	vendor('Itaoke.TopClient');
    	vendor('Itaoke.RequestCheckUtil');
    	vendor('Itaoke.Logger');
    	$top = new \TopClient;
    	$top->appkey = $this->_tbconfig['app_key'];
    	$top->secretKey = $this->_tbconfig['app_secret'];
    	return $top;
    }
} 
?>
